/*
 * Image.cpp
 *
 *  Created on: 29 mar 2010
 *      Author: Andreas
 */

#include "Image.h"
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_opengl.h>
#include <string>
#include "ScreenManager.h"
#include "SpriteAnimation.h"
#include <iostream>

Image::Image(string file) {

	if (file != "") {
		loadImage(file);
	}
	angle = 0;
	scaleX = 1.0;
	scaleY = -1.0;

}

void Image::loadImage(string file) {

	surface = IMG_Load(file.c_str());

	width=surface->w;
	height=surface->h;


	glPixelStorei(GL_UNPACK_ALIGNMENT,4);

	glGenTextures(1,&texture);
	glBindTexture(GL_TEXTURE_2D,texture);

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	SDL_PixelFormat *fmt = surface->format;

	//if there is alpha
	if (fmt->Amask)
	{
		gluBuild2DMipmaps(GL_TEXTURE_2D,4,width,height,GL_RGBA,GL_UNSIGNED_BYTE,surface->pixels);
	}
	else // no alpha
	{
		gluBuild2DMipmaps(GL_TEXTURE_2D,3,width,height,GL_RGB,GL_UNSIGNED_BYTE,surface->pixels);
	}

}

void Image::draw(SpriteAnimation *animation) {
	float picw=0,picx=0,pich=0,picy=0;
	float quadx1=0,quadx2=0,quady1=0,quady2=0;

	if(animation!=NULL) {
		SDL_Rect temp;
		temp =animation->getFrame();

		picw= (float)((temp.x+temp.w)/(float)width);
		picx= (float)(temp.x/(float)width);
		pich= (float)((temp.y+temp.h)/(float)height);
		picy= (float)(temp.y/(float)height);

		quadx2= (float)(ScreenManager::screenScale(animation->getWidth()/2.0));
		quadx1= (float)(ScreenManager::screenScale(0-animation->getWidth()/2.0));
		quady2= (float)(ScreenManager::screenScale(animation->getHeight()/2.0));
		quady1= (float)(ScreenManager::screenScale(0-animation->getHeight()/2.0));
	} else {
		picw= 1;
		picx= 0;
		pich= 1;
		picy= 0;

		quadx2= (float)(ScreenManager::screenScale(width/2.0));
		quadx1= (float)(ScreenManager::screenScale(0-width/2.0));
		quady2= (float)(ScreenManager::screenScale(height/2.0));
		quady1= (float)(ScreenManager::screenScale(0-height/2.0));
	}

	//bind texture
	glBindTexture(GL_TEXTURE_2D, texture );

	glLoadIdentity();
	glTranslatef(ScreenManager::screenXpos(xpos,fixedToScr), ScreenManager::screenYpos(ypos,fixedToScr), 0.0);

	glRotatef(-angle,0.0f,0.0f,1.0f);
	glScalef(scaleX, scaleY, 1.0f);

	//draw the quad
	glBegin(GL_QUADS);
	//Top-left vertex (corner)
	glTexCoord2f(picx, pich);
	glVertex2f(quadx1, quady1);

	//Bottom-left vertex (corner)
	glTexCoord2f(picx, picy);
	glVertex2f(quadx1, quady2);

	//Bottom-right vertex (corner)
	glTexCoord2f(picw, picy);
	glVertex2f(quadx2, quady2);

	//Top-right vertex (corner)
	glTexCoord2f(picw, pich);
	glVertex2f(quadx2, quady1);
	glEnd();
}

Image::~Image() {
	SDL_FreeSurface(surface);
}

string Image::getFileName() {
	return file;
}

SDL_Surface *Image::getSurface() {
	return surface;
}

int Image::getWidth() {
	return width;
}

int Image::getHeight() {
	return height;
}

GLuint Image::getTexture() {
	return texture;
}

void Image::moveTo(float x, float y) {
	xpos = x;
	ypos = y;
}

void Image::moveTo(int x, int y) {
	xpos = x;
	ypos = y;
}

void Image::rotate(double da) {
	angle+=da;
}
